package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.entity.Dish;
import com.tjetc.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("admin/dish")
public class DishListController {
    @Autowired
    DishService dishService;

    /* 按照输入的字段查询相应的菜品 */
    @RequestMapping("page")
    public JsonResult selectDish2Page(@RequestParam(value = "dishName", required = false) String dishName,
                                      @RequestParam(value = "merchantName", required = false) String merchantName,
                                      @RequestParam(value = "upPrice", required = false) Integer upPrice,
                                      @RequestParam(value = "downPrice", required = false) Integer downPrice,
                                      @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                      @RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize){
        return new JsonResult<>(0, "",
                 dishService.select2Page(dishName, merchantName, upPrice,
                                        downPrice, pageNum, pageSize));
    }

    /**
     * 批量移除订单ids
     * @param ids 订单id的列表
     * @return 返回前端处理数据
     */

    @RequestMapping("remove")
    public JsonResult removeDishes(@RequestBody List<Integer> ids){
        return new JsonResult<>(0, "", dishService.deleteDishes(ids));
    }

    /**
     * 添加菜品
     * @param dish 指定添加的菜品
     * @return 返回前端处理数据，显示是否添加成功
     */
    @RequestMapping("add")
    public JsonResult addDish(Dish dish){
        return new JsonResult(0, "", dishService.insert(dish));
    }

    /**
     * 选择全部的菜品
     * @return 返回前端处理数据，包含查询到的所有的菜品列表
     */
    @RequestMapping("selectAll")
    public JsonResult selectAll(){
        return new JsonResult<>(0, "", dishService.selectAll());
    }

    /**
     * 根据id查询菜品，返回前端处理Json对象
     * @param id 指定需要查询的菜品id
     * @return 返回包含菜品对象的Json结果对象
     */

    @RequestMapping("checkById")
    public JsonResult checkById(@RequestParam("dishId") Integer id) {
        return new JsonResult<>(0, "", dishService.selectById(id));
    }

    /**
     * 根据菜品的名称查询菜品的列表
     * @param name 传入指定的菜品
     * @return 返回查询到的菜品
     */

    @RequestMapping("checkByName")
    public JsonResult checkByName(@RequestParam("dishName") String name) {
        return new JsonResult<>(0, "", dishService.selectByName(name));
    }

    // 更新菜品的controller，注意前端传到后端的时间为Long类型
    // 后端传到前端的时间为String类型
    @RequestMapping("update")
    public JsonResult update(Dish dish){
        return new JsonResult<>(0, "", dishService.update(dish));
    }

    /**
     * 批量通过菜品的审核
     * @param ids 传入的菜品的id列表
     * @return 返回包含批量修改之后的int类型信息的Json结果对象
     */
    @RequestMapping("dishPass")
    public JsonResult dishPass(@RequestBody List<Integer> ids){
        return new JsonResult<>(0, "", dishService.passDishes(ids));
    }
}
